package day17_Inet.demo05_4copy;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Scanner;

public class SendCopy {
    public static void main(String[] args) throws Exception{
        Scanner scanner = new Scanner(System.in);
        DatagramSocket ds = new DatagramSocket(8888);
        while (true){
            //发送数据
            String s = scanner.next();
            byte[] bytes = s.getBytes();
            InetAddress ip = InetAddress.getByName("192.168.3.172");
            DatagramPacket dp = new DatagramPacket(bytes, 0, bytes.length, ip, 9999);
            ds.send(dp);
            if (s.equals("bye")){
                break;
            }
            //接收数据
            byte[] bytes1 = new byte[1024];
            DatagramPacket dp1 = new DatagramPacket(bytes1, 0, bytes1.length);
            ds.receive(dp1);
            String s1 = new String(dp1.getData(), 0, dp1.getLength());
            System.out.println("接收的数据为"+s1);
            if (s1.equals("bye")){
                break;
            }
        }
        ds.close();
    }
}
